package com.ds.core.support.qiniu;

import com.qiniu.cdn.CdnManager;
import com.qiniu.cdn.CdnResult;
import com.qiniu.common.QiniuException;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

public class Cdn {
	public static void refreshFiles() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 待刷新的链接列表
		String[] urls = new String[] { "http://javasdk.qiniudn.com/gopher1.jpg",
				"http://javasdk.qiniudn.com/gopher2.jpg",
				// ....
		};
		try {
			// 单次方法调用刷新的链接不可以超过100个
			CdnResult.RefreshResult result = c.refreshUrls(urls);
			System.out.println(result.code);
			// 获取其他的回复内容
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void refreshDir() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 待刷新的目录列表，目录必须以 / 结尾
		String[] dirs = new String[] { "http://javasdk.qiniudn.com/gopher1/", "http://javasdk.qiniudn.com/gopher2/",
				// ....
		};
		try {
			// 单次方法调用刷新的目录不可以超过10个，另外刷新目录权限需要联系技术支持开通
			CdnResult.RefreshResult result = c.refreshDirs(dirs);
			System.out.println(result.code);
			// 获取其他的回复内容
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void Preloading() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 待预取的文件链接
		String[] urls = new String[] { "http://javasdk.qiniudn.com/gopher1.jpg",
				"http://javasdk.qiniudn.com/gopher2.jpg",
				// ...
		};
		try {
			// 单次调用方法预取的链接数量不得超过100个
			CdnResult.PrefetchResult result = c.prefetchUrls(urls);
			System.out.println(result.code);
			// 获取其他的回复内容
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void flux() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 域名列表
		String[] domains = new String[] { "img1.example.com", "img2.example.com", };
		// 开始和结束日期
		String fromDate = "2017-01-02";
		String toDate = "2017-01-10";
		// 数据粒度，支持的取值为 5min ／ hour ／day
		String granularity = "day";
		try {
			CdnResult.FluxResult fluxResult = c.getFluxData(domains, fromDate, toDate, granularity);
			// 处理得到的结果数据
			// ...
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void Bandwidth() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 域名列表
		String[] domains = new String[] { "img1.example.com", "img2.example.com", };
		// 开始和结束日期
		String fromDate = "2017-01-02";
		String toDate = "2017-01-10";
		// 数据粒度，支持的取值为 5min ／ hour ／day
		String granularity = "day";
		try {
			CdnResult.BandwidthResult bandwidthResult = c.getBandwidthData(domains, fromDate, toDate, granularity);
			// 处理得到的结果数据
			// ...
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void getLogUrl() {
		String accessKey = "your access key";
		String secretKey = "your secret key";
		Auth auth = Auth.create(accessKey, secretKey);
		CdnManager c = new CdnManager(auth);
		// 域名列表
		String[] domains = new String[] { "img1.example.com", "img2.example.com", };
		// 具体日期
		String logDate = "2017-01-02";
		try {
			CdnResult.LogListResult logListResult = c.getCdnLogList(domains, logDate);
			// 处理得到的结果数据
			// ...
		} catch (QiniuException e) {
			System.err.println(e.response.toString());
		}
	}

	public static void timeStampUrl() {
		String host = "http://video.example.com";
		String fileName = "基本概括.mp4";
		// 查询参数
		StringMap queryStringMap = new StringMap();
		queryStringMap.put("name", "七牛");
		queryStringMap.put("year", 2017);
		queryStringMap.put("年龄", 28);
		// 链接过期时间
		long deadline = System.currentTimeMillis() / 1000 + 3600;
		// 签名密钥，从后台域名属性中获取
		String encryptKey = "xxx";
		String signedUrl;
		try {
			signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName, queryStringMap, encryptKey, deadline);
			System.out.println(signedUrl);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
